﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using VietThuongLMS.ViewModels;
using VietThuongLMS.Services;

//Author: Kha
namespace VietThuongLMS.Areas.Admin.Controllers
{
    public class AccountancyController : Controller
    {
        //
        // GET: /Admin/Accountancy/

        [Authorize(Roles = "Admin,NhanVien")]
        public ActionResult TuitionFee()
        {
            ViewData["Lop"] = new SelectList(new LopHocService().GetLopHocList(),"ID", "TenLopHoc","");
            List<ChiNhanh> cnl = ChiNhanhService.GetChiNhanhList();
            ChiNhanh cn = new ChiNhanh(); cn.TenChiNhanh = "Tất cả"; cn.ID = 0;
            cnl.Add(cn);
            ViewData["ChiNhanh"] = new SelectList(cnl, "ID", "TenChiNhanh", 0);
            return View();
        }

        [Authorize(Roles = "Admin,NhanVien")]
        public ActionResult Search(string kw1, string kw2, string kw3)
        {
            int page = 1;
            if (Request["p"] != null)
            {
                page = int.Parse(Request["p"].ToString());
            }

            var acvm = AccountService.GetTaiKhoanHocVienWithPagination(kw1, kw2, kw3, 10, page);
            //phân trang
            ViewData["pageLinks"] = Utils.GeneralHTMLString.HTMLForJavaScriptPagination(acvm.Total, 10, page, "searchingRequest");
            return PartialView("pvAccountSearchingResult", acvm);
        }

        [Authorize(Roles = "Admin,NhanVien")]
        public ActionResult ClassSearchByAccount(int id)
        {
            int page = 1;
            if (Request["p"] != null)
            {
                page = int.Parse(Request["p"].ToString());
            }
            List<int> args = new List<int>();
            args.Add(id);
            var hv_lvm = HocVien_LopService.GetHocVien_LopByMaTaiKhoanWithPagination(id, 10, page);
            ViewData["pageLinks"] = Utils.GeneralHTMLString.HTMLForJavaScriptPagination(hv_lvm.TotalMoRong, 10, page, "searchingClassRequest", args);
            return PartialView("pvClassSearchingResult", hv_lvm);
        }

        [Authorize(Roles = "Admin,NhanVien")]
        [HttpPost]
        public ActionResult UpdatePayTuitionFee(int id, bool op, string tienphaidong)
        {
            string resultString = "";
            if (op == true)
            {
                bool rs = HocVien_LopService.CapNhatDongHocPhi(id, 1, tienphaidong);
                if (rs == true)
                    resultString = "Cập nhật thành công.";
                else
                    resultString = "Cập nhật thất bại";
            }
            else
            {
                bool rs = HocVien_LopService.CapNhatDongHocPhi(id, 0, tienphaidong);
                if (rs == true)
                    resultString = "Cập nhật thành công.";
                else
                    resultString = "Cập nhật thất bại";
            }
            return this.Content(resultString);
        }

        [Authorize(Roles = "Admin,NhanVien")]
        public ActionResult Create(int id)
        {
            ViewData["id"] = id;
            return View();
        }

        [Authorize(Roles = "Admin,NhanVien")]
        [HttpPost, ValidateInput(false)]
        public ActionResult Create(HocVien_Lop model)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    HocVien_Lop data = new HocVien_Lop();
                    data.ID = model.ID;
                    data.TienDaDong = model.TienDaDong;
                    data.LyDo = model.LyDo;

                    bool result = HocVien_LopService.UpdateHocVienLopHocPhi(data);
                    if (!result)
                        return RedirectToAction("Create", "Accountancy", new { area = "Admin", id = model.ID });
                    return RedirectToAction("TuitionFee", "Accountancy", new { area = "Admin" });
                }
                catch (Exception ex)
                {

                }
            }
            return View(model);
        }
    }
}
